package com.liang.maogouyuyin.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.liang.maogouyuyin.bean.CasebookBean
import com.liang.maogouyuyin.local.LocalRepository
import kotlinx.coroutines.launch

class CasebookViewModel: ViewModel() {
    private val _casebookList = MutableLiveData<ArrayList<CasebookBean>>().apply { value = ArrayList() }
    val casebookList get() = _casebookList

    fun saveCasebook(name: String, date: Long, hospital: String, casebook: String, content: String, image: String) {
        val bean = CasebookBean(
            name, date, hospital, casebook, content, image
        )
        viewModelScope.launch {
            LocalRepository.saveCasebook(bean)
        }
    }

    fun deleteCasebook(bean: CasebookBean) {
        viewModelScope.launch {
            LocalRepository.deleteCasebook(bean)
        }
    }

    fun updateCasebook(bean: CasebookBean) {
        viewModelScope.launch {
            LocalRepository.updateCasebook(bean)
        }
    }

    fun loadCasebookByName(name: String) {
        viewModelScope.launch {
            _casebookList.value = LocalRepository.loadCasebook(name)
        }
    }
}